home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2000 January / maximum-cd-2000-01.iso / Dreamweaver2 / data1.cab / Program_Files / Configuration / Behaviors / Actions / Timeline / Go To Timeline Frame.js < prev    next >
Encoding:
JavaScript  |  1999-02-23  |  7.3 KB  |  192 lines

  1. //*************** GLOBALS VARS *****************
  2.  
  3. //******************* BEHAVIOR FUNCTION **********************
  4.  
  5. //Jumps a timeline to a new frame.
  6. //Accepts the following arguments:
  7. //  tmLnName - the name of the timeline (ex: Timeline1)
  8. //  fNew     - the frame number to jump to
  9. //  numGotos - (optional) the number of times to jump there
  10. //
  11. //Designed to work in conjunction with Dreamweaver's Timeline Inspector.
  12. //The Timeline Inspector creates a JS function called MM_initTimelines(),
  13. //which puts all the timeline settings in a multidimensional array, saved
  14. //into a document property called document.MM_Time.
  15. //
  16. //My function initializes the timeline by calling MM_initTimelines.
  17. //Then it checks the data arrays and sets all sprites to their new positions
  18. //and sets other properties. For behavior sprites it evals the behavior call.
  19. //If numGotos is set, this function will disable itself. For example, if you call:
  20. //  MM_timelineGoto("Timeline1", "1", "3");
  21. //this function will work the first and second time, but not the third, so
  22. //that the timeline can continue after "3" loops.
  23.  
  24. function MM_timelineGoto(tmLnName, fNew, numGotos) { //v2.0
  25.   //Copyright 1997 Macromedia, Inc. All rights reserved.
  26.   var i,j,tmLn,props,keyFrm,sprite,numKeyFr,firstKeyFr,lastKeyFr,propNum,theObj;
  27.   if (document.MM_Time == null) MM_initTimelines(); //if *very* 1st time
  28.   tmLn = document.MM_Time[tmLnName];
  29.   if (numGotos != null)
  30.     if (tmLn.gotoCount == null) tmLn.gotoCount = 1;
  31.     else if (tmLn.gotoCount++ >= numGotos) {tmLn.gotoCount=0; return}
  32.   jmpFwd = (fNew > tmLn.curFrame);
  33.   for (i = 0; i < tmLn.length; i++) {
  34.     sprite = (jmpFwd)? tmLn[i] : tmLn[(tmLn.length-1)-i]; //count bkwds if jumping back
  35.     if (sprite.charAt(0) == "s") {
  36.       numKeyFr = sprite.keyFrames.length;
  37.       firstKeyFr = sprite.keyFrames[0];
  38.       lastKeyFr = sprite.keyFrames[numKeyFr - 1];
  39.       if ((jmpFwd && fNew<firstKeyFr) || (!jmpFwd && lastKeyFr<fNew)) continue; //skip if untouchd
  40.       for (keyFrm=1; keyFrm<numKeyFr && fNew>=sprite.keyFrames[keyFrm]; keyFrm++);
  41.       for (j=0; j<sprite.values.length; j++) {
  42.         props = sprite.values[j];
  43.         if (numKeyFr == props.length) propNum = keyFrm-1 //keyframes only
  44.         else propNum = Math.min(Math.max(0,fNew-firstKeyFr),props.length-1); //or keep in legal range
  45.         if (sprite.obj != null) {
  46.           if (props.prop2 == null) sprite.obj[props.prop] = props[propNum];
  47.           else        sprite.obj[props.prop2][props.prop] = props[propNum];
  48.       } }
  49.     } else if (sprite.charAt(0)=='b' && fNew == sprite.frame) eval(sprite.value);
  50.   }
  51.   tmLn.curFrame = fNew;
  52.   if (tmLn.ID == 0) eval('MM_timelinePlay(tmLnName)');
  53. }
  54.  
  55.  
  56. //******************* API **********************
  57.  
  58.  
  59. //Checks for the existence of timelines.
  60. //If none exist, returns false so this Action is grayed out.
  61.  
  62. function canAcceptBehavior(){
  63.   var allScripts,i,theScript,timelineExists;
  64.  
  65.   timelineExists = false;
  66.   allScripts = getObjectTags("document","script");
  67.   for (i in allScripts) {
  68.     theScript = ""+allScripts[i];
  69.     if (theScript.indexOf("function MM_initTimelines") != -1) {
  70.       timelineExists = true;
  71.       break;
  72.   } }
  73.   return (timelineExists);
  74. }
  75.  
  76.  
  77.  
  78. //Returns a Javascript function to be inserted in HTML head with script tags.
  79.  
  80. function behaviorFunction(){
  81.   return "MM_timelineGoto";
  82. }
  83.  
  84.  
  85.  
  86. //Returns fn call to insert in HTML tag <TAG... onEvent='thisFn(arg)'>
  87. //Returns two args: the selected timeline name, and the frmNum to goto.
  88.  
  89. function applyBehavior(x,y,theEvent) {
  90.   var menuIndex, timelineName, frmNum, numGotos, curFrm;
  91.   var eventFromTI = "onFrame";  //event prefix passed to applyBehavior from Timeline Inspector
  92.  
  93.   menuIndex = document.theForm.menu.selectedIndex;  //get menu selection index
  94.   timelineName = document.theForm.menu.options[menuIndex].text; //gets selected string
  95.   frmNum = parseInt(document.theForm.frmNum.value); //try and get frame num
  96.   if ((document.theForm.frmNum.value != ""+frmNum) ||  //if not a number
  97.       (frmNum < 1))                                    //or if < 1
  98.     return MSG_NegFrameNum;
  99.   else
  100.     if (document.theForm.numGotos.value) { //if not empty
  101.       numGotos = parseInt(document.theForm.numGotos.value); //try and get numGotos
  102.       if ((document.theForm.numGotos.value != ""+numGotos) || //if not a number
  103.           (numGotos < 1))                                     //or if < 1
  104.            return MSG_NegGotoNum;
  105.  
  106.       else { //user entered a Loop value
  107.         if (theEvent.indexOf(eventFromTI) == -1) { //if event is not from Timeline Inspector
  108.           document.theForm.numGotos.value == ""; //clear loop value
  109.           return MSG_LoopNotFromTI;
  110.         }
  111.         curFrm = parseInt(theEvent.substring(eventFromTI.length,theEvent.length)); //get frame #
  112.         if (frmNum > curFrm) { //if not jumping backward
  113.           document.theForm.numGotos.value == ""; //clear loop value
  114.           return MSG_LoopNotBackward;
  115.         } 
  116.         else { //everything's okay, allow loop value
  117.           return "MM_timelineGoto('"+timelineName+"','"+frmNum+"','"+(numGotos-1)+"')";
  118.         }
  119.       }
  120.     }
  121.     else return "MM_timelineGoto('"+timelineName+"','"+frmNum+"')";
  122. }
  123.  
  124.  
  125.  
  126. //Passed the function call above, extracts the args and reloads the UI.
  127. //With arg timelineName, scans the menu for a matching item, and selects it.
  128. //If the name is not found, it gives an error msg.
  129. //With arg frmNum, stores in text input.
  130.  
  131. function inspectBehavior(upStr){
  132.   var timelineName,menuLength,i;
  133.   var argArray = new Array;
  134.   var found = false;
  135.  
  136.   argArray = extractArgs(upStr); //get args
  137.   if (argArray.length > 2) {  //should be 3 or 4 args (first arg is fn call, ignored)
  138.     timelineName = argArray[1];
  139.     menuLength = document.theForm.menu.options.length;
  140.     for (var i=0; i<menuLength; i++) {  //search menu for matching timeline name
  141.       if (document.theForm.menu.options[i].text == timelineName) { //if found
  142.         document.theForm.menu.selectedIndex = i;  //make it selected
  143.         found = true;
  144.         break;
  145.       }
  146.     }
  147.     if (!found) alert(errMsg(MSG_TimelineNotFound,timelineName));
  148.     document.theForm.frmNum.value = argArray[2];
  149.     if (argArray.length > 3) document.theForm.numGotos.value = parseInt(argArray[3])+1;
  150.   }
  151. }
  152.  
  153.  
  154.  
  155. //***************** LOCAL FUNCTIONS  ******************
  156.  
  157.  
  158. //Load the select menu with timeline names.
  159.  
  160. function initializeUI(){
  161.   var i,j,startPos,endPos,theName,theScript;
  162.   var menuIndex = 0;
  163.   var allScripts = new Array;
  164.  
  165.   allScripts = getObjectTags("document","script");
  166.   for (i in allScripts) {
  167.     theScript = ""+allScripts[i];
  168.     if (theScript.indexOf("function MM_initTimelines") != -1) {
  169.       j = theScript.indexOf('].MM_Name');
  170.       while (j != -1) {
  171.         startPos = theScript.indexOf('"',++j);
  172.         endPos = theScript.indexOf('"',++startPos);
  173.         if (0 < startPos && startPos < endPos) {
  174.           theName = theScript.substring(startPos,endPos);
  175.           document.theForm.menu.options[menuIndex++] = new Option(theName);
  176.         }
  177.         j = theScript.indexOf('].MM_Name',j+1);
  178.       }
  179.     }
  180.   }
  181.  
  182.   document.theForm.frmNum.focus(); //set focus on textbox
  183.   document.theForm.frmNum.select(); //set insertion point into textbox
  184. }
  185.  
  186.  
  187.  
  188. //**************** GENERIC FUNCTIONS ****************
  189.  
  190. //function extractArgs(upStr){
  191. //function errMsg() {
  192.